//
//  LPTPreManageViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/5.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTPreManageViewController.h"
#import "LPTPreManagerTableViewCell.h"
#import "LPTprescriptionViewController.h"

@interface LPTPreManageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong)LPTHoriScrollView *horiView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property (nonatomic, weak)UIButton *addBtn;
@property (nonatomic, copy)NSString *preType;//classic经典ordinary普通secret秘方
@property (nonatomic, strong)NSIndexPath *indexPath;

@end

@implementation LPTPreManageViewController

+(instancetype)instantiate
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
    LPTPreManageViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"LPTPreManageViewController"];
    return vc;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.dataSource = [[NSMutableArray alloc] init];
    
    [self initSubViews];
    
    self.preType = @"classic";
    
    if (self.type == kSelectType) {
        self.title = @"药方选择";
    }else{
        self.title = @"药方管理";
    }
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getPrescriptListWithType:self.preType];
    }];
    
    self.tableView.mj_header = header;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (!self.addBtn && self.type == kNormalType) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"pre__btn_add"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addPrescriptionAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.parentViewController.view addSubview:button];
        self.addBtn = button;
        [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.trailing.mas_equalTo(@(-30));
            make.bottom.mas_equalTo(@(-60));
            make.size.mas_equalTo(CGSizeMake(40, 40));
        }];
    }
    
    [self getPrescriptListWithType:self.preType];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    [self.addBtn removeFromSuperview];
    
}

//获取处方列表
-(void)getPrescriptListWithType:(NSString *)type
{
    LPTBaseRequest *request =  [[LPTBaseRequest alloc] initWithApi:@"/api/recipe_list" withBody:@{@"page":@(1),@"size":@(1000),@"type":self.preType} withMethod:YTKRequestMethodGET];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        
        if ([data[@"current_page"] integerValue] == 1) {
            [self.dataSource removeAllObjects];
        }
        
        NSArray *preArray = data[@"recipes"];
        [self.dataSource addObjectsFromArray:preArray];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
    }];
}

-(void)addPrescriptionAction:(UIButton *)button
{
    [self performSegueWithIdentifier:@"pushToAddPrescriptionVC" sender:nil];
}

-(void)initSubViews
{
    
    self.horiView = [[LPTHoriScrollView alloc] initWithTitleArray:@[@"经典处方",@"普通处方",@"我的秘方"] frame:CGRectMake(0, kTopBarHeight, kScreenWidth, 46) selectBlock:^(NSInteger index) {
        
        if (index == 0) {
            self.preType = @"classic";
        }else if (index == 1){
            self.preType = @"ordinary";
        }else{
            self.preType = @"secret";
        }
        
        [self getPrescriptListWithType:self.preType];
        
    }];
    [self.view addSubview:self.horiView];
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 80;
    
}

#pragma mark - UITableViewDataSource & Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LPTPreManagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LPTPreManagerTableViewCell" forIndexPath:indexPath];
    [cell refreshWithDic:self.dataSource[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.indexPath = indexPath;
    
    if (self.type == kNormalType) {
        
        [self performSegueWithIdentifier:@"pushToPrescriptionDetailVC" sender:nil];
    }else{
        
        NSDictionary *dic = self.dataSource[self.indexPath.row];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(preManageViewControllerDidSelectItem:)]) {
            [self.delegate preManageViewControllerDidSelectItem:dic];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSDictionary *dic = self.dataSource[self.indexPath.row];
    if ([segue.identifier isEqualToString:@"pushToPrescriptionDetailVC"]) {
        
        LPTprescriptionViewController *vc = segue.destinationViewController;
        vc.preId = dic[@"id"];
    }
}

@end
